﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcFinalTest.Models;
using MvcFinalTest.Data;
using System.Collections.ObjectModel;

namespace MvcFinalTest.Controllers
{
    public class BranchController : Controller
    {
        //
        // GET: /Branch/

        public ActionResult Index()
        {
            Collection<BranchModel> branch = BranchRepository.GetAll();
            ViewData["BranchData"] = branch.Count().ToString();
            return View(branch);
        }

        //
        // GET: /Branch/Details/5

        public ActionResult Details(int id)
        {
            BranchModel branch = BranchRepository.GetBranchById(id);
            return View(branch);
        }

        //
        // GET: /Branch/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Branch/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            BranchModel branch = new BranchModel();
            try
            {
                //if (collection["ZipCode"].Length != 5)
                //{
                //    ModelState.AddModelError("ZipCode", "Zip Code Must Have 5 Characters");
                //}
                //else
                //{
                    if (TryUpdateModel(branch))
                    {
                        if (ModelState.IsValid)
                        {
                            BranchRepository.Add(branch);
                            return RedirectToAction("Index");
                        }
                    }
                //}
            }
            catch
            {
                return View(branch);
            }
            return View(branch);
        }
        
        //
        // GET: /Branch/Edit/5
 
        public ActionResult Edit(int id)
        {
            BranchModel branch = BranchRepository.GetBranchById(id);
            return View(branch);
        }

        //
        // POST: /Branch/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            BranchModel branch = BranchRepository.GetBranchById(id);
            try
            {
                //if (collection["ZipCode"].Length != 5)
                //{
                //    ModelState.AddModelError("ZipCode", "Zip Code Must Have 5 Characters");
                //}
                //else
                //{
                    if (TryUpdateModel(branch))
                    {
                        if (ModelState.IsValid)
                        {
                            BranchRepository.Edit(branch);
                            return RedirectToAction("Index");
                        }
                    }
                //}
            }
            catch
            {
                return View(branch);
            }
            return View(branch);
        }

        //
        // GET: /Branch/Delete/5
 
        public ActionResult Delete(int id)
        {
            BranchRepository.Delete(id);
            return RedirectToAction("Index");
        }

    }
}
